package com.pod5.skype.worker;

import java.awt.List;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
 
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
 
public class ModifyXMLFileWorker {
 
private LinkedList<String> rawIPData;
private String filepath;


public ModifyXMLFileWorker(String filePath) {
	// TODO Auto-generated method stub
	this.filepath = filePath; 
	
}
 
private void parseHostCache(String textContent) {
	// TODO Auto-generated method stub
	String[] splitAddr = textContent.split("50041050200");
	
	for(int i =1; i < splitAddr.length; i++)
	{
		rawIPData.add(splitAddr[i]);
	}
	
} 



	//Built for one machine parsing and storing information.
 public LinkedList<String> modifySkypeHostCache(String nextIP) {

	 rawIPData = new LinkedList<String>();
	 
   try{
	 
	 DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
	 DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
	 Document doc = docBuilder.parse(filepath);
 
	 //Get the root element
	 Node company = doc.getFirstChild(); 
 
	  
         //Get the staff element by tag name directly
         Node connection = doc.getElementsByTagName("Connection").item(0);
 
    	 NodeList list = connection.getChildNodes();
    	 
    	 for (int i =0; i<list.getLength();i++){
    		 Node node = list.item(i);
     
    		 //get the salary element, and update the value    		 
    		 if("HostCache".equals(node.getNodeName())){
    			 parseHostCache(node.getTextContent());
    			 
    			 //need to send raw info to server
    			 

    				 node.setTextContent("41C801050041050041050200" + nextIP);
    				 break;

    		 }
    	 }
 

     //write the content into xml file
     TransformerFactory transformerFactory = TransformerFactory.newInstance();
     Transformer transformer = transformerFactory.newTransformer();
     DOMSource source = new DOMSource(doc);
     StreamResult result =  new StreamResult(new File(filepath));
     transformer.transform(source, result);
 
     System.out.println("Done");
 
   }catch(ParserConfigurationException pce){
	 pce.printStackTrace();
   }catch(TransformerException tfe){
	 tfe.printStackTrace();
   }catch(IOException ioe){
	 ioe.printStackTrace();
   }catch(SAXException sae){
	 sae.printStackTrace();
   }
     
   return rawIPData;
  
 }





}
